-- @owner:yangjian
-- @date:2024-4-6
-- @testpoint:1.去除大对象使用限制，测试lowrite函数使用，合理报错
 
--清理环境
CREATE OR REPLACE FUNCTION f_opengauss_large_object_function_case0006_1()  
RETURNS VOID AS 
$$
BEGIN  
    BEGIN
    PERFORM lo_unlink(100);
    EXCEPTION
        WHEN others THEN
            NULL;
    end;
	BEGIN
    PERFORM lo_unlink(-2147483648);
    EXCEPTION
        WHEN others THEN
            NULL;
    end;
	BEGIN
    PERFORM lo_unlink(2147483647);
    EXCEPTION
        WHEN others THEN
            NULL;
    end;
	BEGIN
    PERFORM lo_unlink(-1);
    EXCEPTION
        WHEN others THEN
            NULL;
    end;
	BEGIN
    PERFORM lo_unlink(200);
    EXCEPTION
        WHEN others THEN
            NULL;
    end;
	BEGIN
    PERFORM lo_unlink(1);
    EXCEPTION
        WHEN others THEN
            NULL;
    end;
	BEGIN
    PERFORM lo_unlink(2);
    EXCEPTION
        WHEN others THEN
            NULL;
    end;
END;  
$$
LANGUAGE plpgsql;
/

select f_opengauss_large_object_function_case0006_1();

--环境准备
drop table if exists t_opengauss_large_object_function_case0006_2 cascade;
CREATE TABLE t_opengauss_large_object_function_case0006_2 (a int);
insert into t_opengauss_large_object_function_case0006_2 values (1);
insert into t_opengauss_large_object_function_case0006_2 values (2);

--step1:创建表存储函大对象信息;expect:成功
drop table if exists t_opengauss_large_object_function_case0006_1 cascade;
CREATE TABLE t_opengauss_large_object_function_case0006_1 (loid oid,fd int);

--step2:测试fd参数为存在值;expect:成功
INSERT INTO t_opengauss_large_object_function_case0006_1 (loid) SELECT lo_create(100);
begin;
SELECT lo_open(cast(100 as oid),CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaa');
end;
/

--step3:测试fd参数为不存在值;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (1,'aaaaaa');
end;
/

--step4:测试fd参数为异常值;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite ('a','aaaaaa');
end;
/
end;
--step5:测试fd参数为int4边界值;expect:失败，报不存在
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (2147483647,'aaaaaa');
end;
/
end;
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (-2147483648,'aaaaaa');
end;
/
end;
--step6:测试fd参数超int4边界值;expect:失败，报超限制
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (2147483648,'aaaaaa');
end;
/
end;
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (-2147483649,'aaaaaa');
end;
/
end;
--step7:测试fd参数为空值;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (null,'aaaaaa');
end;
/
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite ('','aaaaaa');
end;
/

--step8:测试data参数为非bytea类型;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,1);
end;
/
end;
--step9:测试data参数为空;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'');
end;
/
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,null);
end;
/

--step10:测试data参数1g大小;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,cast(REPEAT('a', 65536) as bytea));
end;
/

--清理环境
select lo_unlink(loid) from t_opengauss_large_object_function_case0006_1;
select f_opengauss_large_object_function_case0006_1();
drop table if exists t_opengauss_large_object_function_case0006_1 cascade;
drop table if exists t_opengauss_large_object_function_case0006_2 cascade;
drop function f_opengauss_large_object_function_case0006_1();
